package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvResult;

    private String firstNum = "";
    private String secondNum = "";
    private String operator = "";
    private String result = "";
    private String showText = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 从布局文件中获取文本视图
        tvResult = findViewById(R.id.tv_result);

        // 为所有按钮注册点击监听器
        findViewById(R.id.btn_clear_entry).setOnClickListener(this);
        findViewById(R.id.btn_divide).setOnClickListener(this);
        findViewById(R.id.btn_multiply).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_plus).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_sqrt).setOnClickListener(this);
        findViewById(R.id.btn_reciprocal).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String inputText = ((TextView) view).getText().toString();

        List<Integer> fourOperations = Arrays.asList(R.id.btn_plus, R.id.btn_minus, R.id.btn_multiply, R.id.btn_divide);
        int id = view.getId();

        if (id == R.id.btn_clear) {
            // 清除
            clear();
        } else if (id == R.id.btn_clear_entry) {
            // 清除输入
            clearEntryOperate();
        } else if (fourOperations.contains(id)) {
            operator = inputText;
            refreshShowText(showText + operator);
            // 四则运算
        } else if (id == R.id.btn_equal) {
            // 等号
            if (!secondNum.equals("")) {
                double calculateResult = calculateFour();
                refreshOperateResult(String.valueOf(calculateResult));
                refreshShowText(result);
            }
        } else if (id == R.id.btn_sqrt) {
            // 根号
            double sqrtResult = Math.sqrt(Double.parseDouble(firstNum));
            refreshOperateResult(String.valueOf(sqrtResult));
            refreshShowText(result);
        } else if (id == R.id.btn_reciprocal) {
            // 倒数
            double reciprocalResult = 1.0 / Double.parseDouble(firstNum);
            refreshOperateResult(String.valueOf(reciprocalResult));
            refreshShowText(result);
        } else {
            // 其它
            if (result.length() > 0 && operator.equals("")) {
                clear();
            }

            if (operator.equals("")) {
                firstNum += inputText;
            } else {
                secondNum += inputText;
            }
            if (showText.equals("0") && !inputText.equals(".")) {
                refreshShowText(inputText);
            } else {
                refreshShowText(showText + inputText);
            }

        }
    }

    private double calculateFour() {
        switch (operator) {
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "×":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            default:
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
    }

    private void clear() {
        refreshOperateResult("");
        refreshShowText("0");
    }

    private void refreshOperateResult(String newResult) {
        result = newResult;
        firstNum = result;
        secondNum = "";
        operator = "";

    }

    private void clearEntryOperate() {
        String tmp = showText.substring(0, showText.length() - 1);
        if (tmp.equals("")) {
            tmp = "0";
        }
        refreshShowText(tmp);
    }

    // 刷新显示文本
    private void refreshShowText(String text) {
        showText = text;
        integerRemoveDot();
        tvResult.setText(showText);
    }

    private void integerRemoveDot() {
        int start = showText.indexOf(".");
        if (start != -1) {
            int end = showText.length();
            String value = showText.substring(start + 1, end);
            if (Double.parseDouble(value) == 0.0) {
                showText = showText.substring(0, start);
            }
        }
    }
}